﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web;

namespace Com.Library.Page
{
	public class ViewManager<PageT> where PageT : WebPage, new ()
	{
		public static string Render(string path)
		{
			return Render(path, null);
		}

		public static string Render(string path, Dictionary<string, object> properties)
		{
			try
			{
				PageT pageHolder = new PageT();
				Control viewControl = (Control)pageHolder.LoadControl(path);
				Type controlType = viewControl.GetType();
				if (properties != null && properties.Count > 0)
				{
					foreach(KeyValuePair<string, object> d in properties)
					{
						controlType.GetProperty(d.Key).SetValue(viewControl, d.Value, null);
					}
				}

				pageHolder.Controls.Add(viewControl);

				StringWriter sw = new StringWriter();
				HttpContext.Current.Server.Execute(pageHolder, sw, false);

				return sw.ToString();
			} catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
